package com.chenyaowei.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.chenyaowei.product.domain.CywProductCategory;
import com.chenyaowei.product.mapper.CywProductCategoryMapper;
import com.chenyaowei.product.service.ICywProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 *
 * @author 陈耀威
 * @date 2023-11-20
 */
@Service
public class CywProductCategoryServiceImpl implements ICywProductCategoryService {

    @Autowired
    private CywProductCategoryMapper cywProductCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param catId 商品分类主键
     * @return 商品分类
     */
    @Override
    public CywProductCategory selectCywProductCategoryByCatId(Long catId) {
        return cywProductCategoryMapper.selectCywProductCategoryByCatId(catId);
    }

    /**
     * 查询商品分类列表
     *
     * @param cywProductCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<CywProductCategory> selectTreeCategoryList(CywProductCategory cywProductCategory) {
        if (cywProductCategory.getCatLevel() != null || cywProductCategory.getName()!= null) {
            return cywProductCategoryMapper.selectCywProductCategoryList(cywProductCategory);
        }
        // 找出所有显示的分类
        cywProductCategory.setShowStatus(0);
        List<CywProductCategory> allCategories = cywProductCategoryMapper.selectCywProductCategoryList(cywProductCategory);
        if (CollectionUtil.isEmpty(allCategories)) {
            return allCategories;
        }
        // 找到所有1级分类 组装成树形分类
        List<CywProductCategory> treeTableCategories = allCategories.stream().filter(item -> item.getCatLevel() == 1).collect(Collectors.toList());
        for (CywProductCategory level1category : treeTableCategories) {
            List<CywProductCategory> level2categories = allCategories.stream().filter(item -> item.getParentCid().equals(level1category.getCatId())).collect(Collectors.toList());
            for (CywProductCategory level2category : level2categories) {
                List<CywProductCategory> level3category = allCategories.stream().filter(item -> item.getParentCid().equals(level2category.getCatId())).collect(Collectors.toList());
                level2category.setChildren(level3category);
            }
            level1category.setChildren(level2categories);
        }
        return treeTableCategories;
    }

    @Override
    public List<CywProductCategory> selectCywProductCategoryList(CywProductCategory cywProductCategory) {
        return cywProductCategoryMapper.selectCywProductCategoryList(cywProductCategory);
    }

    /**
     * 新增商品分类
     *
     * @param cywProductCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertCywProductCategory(CywProductCategory cywProductCategory) {
        return cywProductCategoryMapper.insertCywProductCategory(cywProductCategory);
    }

    /**
     * 修改商品分类
     *
     * @param cywProductCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateCywProductCategory(CywProductCategory cywProductCategory) {
        return cywProductCategoryMapper.updateCywProductCategory(cywProductCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param catIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCywProductCategoryByCatIds(Long[] catIds) {
        return cywProductCategoryMapper.deleteCywProductCategoryByCatIds(catIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param catId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCywProductCategoryByCatId(Long catId) {
        return cywProductCategoryMapper.deleteCywProductCategoryByCatId(catId);
    }

    @Override
    public Long findTopCatelogById(Long catelogId) {
        List<CywProductCategory> list = cywProductCategoryMapper.selectCywProductCategoryList(null);
        CywProductCategory category = list.stream().filter(item -> catelogId.equals(item.getCatId())).findFirst().get();
        //顶级分类 level = 1L
        while (category.getCatLevel() != 1L) {
            Long parentCid = category.getParentCid();
            category = list.stream().filter(item -> parentCid.equals(item.getCatId())).findFirst().get();
        }
        return category.getCatId();
    }

    @Override
    public List<CywProductCategory> findOriginCategories(Long catId) {
        List<CywProductCategory> list = cywProductCategoryMapper.selectCywProductCategoryList(null);
        CywProductCategory source = list.stream().filter(item -> catId.equals(item.getCatId())).findFirst().get();
        List<CywProductCategory> result = new ArrayList<>();
        findOriginCatIds(source, list, result);
        return CollectionUtil.reverse(result);
    }

    private List<CywProductCategory> findOriginCatIds(CywProductCategory source, List<CywProductCategory> list, List<CywProductCategory> result){
        result.add(source);
        CywProductCategory parent = list.stream().filter(item -> item.getCatId().equals(source.getParentCid())).findFirst().orElse(null);
        if (ObjectUtil.isNotEmpty(parent)){
            findOriginCatIds(parent, list, result);
        }
        return result;
    }

}
